package com.sixteengroup.personnelms.controller.personnel;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sixteengroup.personnelms.bean.RecruitmentInfo;
import com.sixteengroup.personnelms.bean.Dept;
import com.sixteengroup.personnelms.bean.vo.pageVo;
import com.sixteengroup.personnelms.service.personnel.DeptAndStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName DeptAndStaffService
 * @Author 陈新予(blank)
 * @Date 2021/8/13
 * @Version 1.0
 */
@RestController
public class DeptAndStaffController {

    @Autowired
    DeptAndStaffService deptAndStaffService;

    @PostMapping("getAllDept/{pageNum}/{pageSize}")
    public Map<String,Object> getAllDept(pageVo pageVo){
        Map<String,Object> map = new ConcurrentHashMap<>();
        try{
            PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
            PageInfo<Dept> midList = new PageInfo<>(deptAndStaffService.getAllDept());
            List<Dept> deptLimit = midList.getList();
            map.put("data",deptLimit);
            map.put("code",200);
            map.put("total",midList.getTotal());
        }catch (Exception e){
            map.put("code",500);
            map.put("msg","服务器出现故障,稍后重试!");
        }
        return map;
    }

    @PostMapping("getAllRecrInfo/{pageNum}/{pageSize}")
    public Map<String,Object> getAllRecrInfo(pageVo pageVo){
        Map<String,Object> map = new ConcurrentHashMap<>();
        try{
            PageHelper.startPage(pageVo.getPageNum(), pageVo.getPageSize());
            PageInfo<RecruitmentInfo> midList = new PageInfo<>(deptAndStaffService.getAllRecInfo());
            List<RecruitmentInfo> recrInfoLimit = midList.getList();
            map.put("data",recrInfoLimit);
            map.put("code",200);
            map.put("total",midList.getTotal());
        }catch (Exception e){
            System.out.println(e.getMessage());
            map.put("code",500);
            map.put("msg","服务器出现故障,稍后重试!");
        }
        return map;
    }

    @GetMapping("deletRecInfo")
    public Map<String,Object> deleteThisInfo(int id){
        Map<String,Object> map = new ConcurrentHashMap<>();
        try{
            deptAndStaffService.deleteRecInfo(id);
            map.put("msg","删除成功!");
            map.put("code",200);
        }catch (Exception e){
            System.out.println(e.getMessage());
            map.put("code",500);
            map.put("msg","服务器出现故障,稍后重试!");
        }
        return map;
    }

    @GetMapping("AddRecInfo")
    public Map<String,Object> AddRecInfo(int id,int res){
        Map<String,Object> map = new ConcurrentHashMap<>();
        try{
            if(res == -1){
                deptAndStaffService.updateRecInfo(id,-1);
                //应聘流程终止
                map.put("msg","流程终止!");
                map.put("code",201);
            }else {
                deptAndStaffService.updateRecInfo(id,res+1);
                map.put("msg","修改成功!");
                map.put("code",200);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            map.put("code",500);
            map.put("msg","服务器出现故障,稍后重试!");
        }
        return map;
    }

    @GetMapping("staffOnboarding")
    public Map<String,Object> staffOnboarding(int userId,String deptName){
        Map<String,Object> map = new ConcurrentHashMap<>();
        try{
            deptAndStaffService.saveStaffDept(userId,deptName);
            map.put("msg","操作成功!");
            map.put("code",200);
        }catch (Exception e){
            System.out.println(e.getMessage());
            map.put("code",500);
            map.put("msg","服务器出现故障,稍后重试!");
        }
        return map;
    }

}
